package cn.stylefeng.guns.modular.manager.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.manager.entity.ArchiveDestruction;
import cn.stylefeng.guns.modular.manager.entity.ArchiveDestructionDetail;
import cn.stylefeng.guns.modular.manager.entity.ArchiveInfo;
import cn.stylefeng.guns.modular.manager.mapper.ArchiveDestructionMapper;
import cn.stylefeng.guns.modular.manager.model.params.ArchiveDestructionParam;
import cn.stylefeng.guns.modular.manager.model.result.ArchiveDestructionResult;
import cn.stylefeng.guns.modular.manager.service.ArchiveDestructionDetailService;
import cn.stylefeng.guns.modular.manager.service.ArchiveDestructionService;
import cn.stylefeng.guns.modular.manager.service.ArchiveInfoService;
import cn.stylefeng.guns.sys.core.constant.state.YesNo;
import cn.stylefeng.guns.utils.ServiceAssert;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 档案销毁信息表 服务实现类
 * </p>
 *
 * @author blesslp
 * @since 2019-11-24
 */
@Service
public class ArchiveDestructionServiceImpl extends ServiceImpl<ArchiveDestructionMapper, ArchiveDestruction> implements ArchiveDestructionService {

    @Autowired private ArchiveDestructionDetailService archiveDestructionDetailService;
    @Autowired private ArchiveInfoService archiveInfoService;
    @Override
    @Transactional
    public void add(ArchiveDestructionParam param){
        //保存销毁单
        ArchiveDestruction entity = getEntity(param);
        entity.setJysj(new Date());
        final String ERROR_TIPS = "销毁单添加失败";
        ServiceAssert.assetTrue(this.save(entity), ERROR_TIPS);

        //保存销毁单关联表
        final String[] archiveInfoIds = param.getArchiveInfoIds();
        if (ToolUtil.isEmpty(archiveInfoIds)) {
            return;
        }
        List<ArchiveDestructionDetail> archiveDestructionDetails = new ArrayList<>();
        for (String archiveInfoId : archiveInfoIds) {
            ArchiveDestructionDetail detail = new ArchiveDestructionDetail();
            detail.setDaid(archiveInfoId);
            detail.setDaxhid(entity.getId());
            archiveDestructionDetails.add(detail);
        }
        ServiceAssert.assetTrue(this.archiveDestructionDetailService.saveBatch(archiveDestructionDetails), ERROR_TIPS);

        //修改涉及的档案的销毁状态
        ServiceAssert.assetTrue(this.archiveInfoService.update(Wrappers.<ArchiveInfo>lambdaUpdate()
                .set(ArchiveInfo::getSfyx, YesNo.NO.getCode())
                .in(ArchiveInfo::getId, archiveInfoIds)
        ), ERROR_TIPS);
    }

    @Override
    @Transactional
    public void delete(ArchiveDestructionParam param){
        final Serializable id = getKey(param);
        boolean isSuccess = this.removeById(id);
        final String TIPS = "删除失败";
        ServiceAssert.assetTrue(isSuccess, TIPS);

        final List<ArchiveDestructionDetail> list = this.archiveDestructionDetailService.lambdaQuery().eq(ArchiveDestructionDetail::getDaxhid, id).list();

        //将所有档案的状态变更为有效
        final List<String> daids = list.parallelStream().map(ArchiveDestructionDetail::getDaid).collect(Collectors.toList());
        isSuccess = this.archiveInfoService.lambdaUpdate()
                .in(ArchiveInfo::getId, daids)
                .set(ArchiveInfo::getSfyx, YesNo.YES.getCode())
                .update();
        ServiceAssert.assetTrue(isSuccess, TIPS);

        //删除所有关联记录
        this.archiveDestructionDetailService.lambdaUpdate()
                .eq(ArchiveDestructionDetail::getDaxhid, id)
                .remove();
    }

    @Override
    public void update(ArchiveDestructionParam param){
        ArchiveDestruction oldEntity = getOldEntity(param);
        ArchiveDestruction newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public ArchiveDestructionResult findBySpec(ArchiveDestructionParam param){
        return null;
    }

    @Override
    public List<ArchiveDestructionResult> findListBySpec(ArchiveDestructionParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(ArchiveDestructionParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(ArchiveDestructionParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private ArchiveDestruction getOldEntity(ArchiveDestructionParam param) {
        return this.getById(getKey(param));
    }

    private ArchiveDestruction getEntity(ArchiveDestructionParam param) {
        ArchiveDestruction entity = new ArchiveDestruction();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
